package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sessions")
public class SessionController {
    @Autowired
    private IUserService userService;

    /**
     * 登录
     * @param email : 邮箱
     * @param password : 密码
     * @return
     */
    @PostMapping
    public JsonResult login(String email, String password){
        JsonResult jsonResult=new JsonResult();
        try {
            User user = userService.login(email, password);
            /*//共享当前用户  前端不方便获取  利用Jsonrresult 返回
             UserContext.setCurrentUser(user);*/
            jsonResult.setUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    @DeleteMapping
    public void logout(){
        userService.logout();
    }
}
